package us.mooresr.aircraftscheduling.client.ui;

import java.util.ArrayList;

import us.mooresr.aircraftscheduling.client.AdminGatekeeper;
import us.mooresr.aircraftscheduling.client.LoggedInGatekeeper;
import us.mooresr.aircraftscheduling.client.LoginStateManager;
import us.mooresr.aircraftscheduling.client.events.UserDeletedEvent;
import us.mooresr.aircraftscheduling.client.place.NameTokens;
import us.mooresr.aircraftscheduling.shared.action.DeleteUserAction;
import us.mooresr.aircraftscheduling.shared.action.DeleteUserResult;
import us.mooresr.aircraftscheduling.shared.action.GetUserDetailAction;
import us.mooresr.aircraftscheduling.shared.action.GetUserDetailResult;
import us.mooresr.aircraftscheduling.shared.action.GetUserList;
import us.mooresr.aircraftscheduling.shared.action.GetUserListResult;
import us.mooresr.aircraftscheduling.shared.model.User;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.annotations.UseGatekeeper;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;

public class UserListPresenter extends 
	Presenter<UserListPresenter.MyView, UserListPresenter.MyProxy> 
	implements UserListUiHandlers {
	
	private final UserDetailPresenter detailDialog;
	private final UserDetailROPresenter viewUserDialog;
	private final DispatchAsync dispatcher;
	private final EventBus eventBus;
	
	private final LoginStateManager loginStateManager;
		

	
	@Inject
	public UserListPresenter(EventBus eventBus, MyView view, MyProxy proxy,
			final UserDetailPresenter detailDialog, final DispatchAsync dispatcher,
			final UserDetailROPresenter viewUserDialog, final LoginStateManager loginStateManager) {
		super(eventBus, view, proxy);
		
		this.detailDialog = detailDialog;
		this.dispatcher = dispatcher;
		this.eventBus = eventBus;
		this.viewUserDialog = viewUserDialog;
		this.loginStateManager = loginStateManager;
		
		getView().setUiHandlers(this);
		
	}

	public interface MyView extends View, HasUiHandlers<UserListUiHandlers> {
		void setData(ArrayList<User> userList);
	}
	

	
	@ProxyStandard
	@NameToken(NameTokens.userlist)
	@UseGatekeeper(LoggedInGatekeeper.class)
	public interface MyProxy extends ProxyPlace<UserListPresenter> {
		
	}
	

	@Override
	protected void revealInParent() {
		GetUserList action;
		
		if (loginStateManager.isAdmin()) {
			action = new GetUserList(true);
		} else {
			action = new GetUserList(false);
		}

		dispatcher.execute(action, new AsyncCallback<GetUserListResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(GetUserListResult result) {
				getView().setData(result.getUserList());
				
				RevealContentEvent.fire(eventBus, MainPagePresenter.TYPE_SetMainContent, UserListPresenter.this);
			}
			
		});
		
		
		
	}





	@Override
	public void createUser() {
		detailDialog.clear();
		addToPopupSlot(detailDialog);
		
	}


	@Override
	public void deleteUser(User user) {
		DeleteUserAction action = new DeleteUserAction(user);
		dispatcher.execute(action,new AsyncCallback<DeleteUserResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(DeleteUserResult result) {
				UserDeletedEvent.fire(getEventBus(), result.getUserOut());
				
			}
			
		});
		
	}


	@Override
	public void editUser(final User user) {
		GetUserDetailAction action = new GetUserDetailAction(user.getUserId());
		dispatcher.execute(action, new AsyncCallback<GetUserDetailResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(GetUserDetailResult result) {
				detailDialog.clear();
				detailDialog.preLoad(user, result.getUserDetail());
				addToPopupSlot(detailDialog);
				
			}
			
		});
		
	}

	@Override
	public void viewUser(final User user) {
		GetUserDetailAction action = new GetUserDetailAction(user.getUserId());
		dispatcher.execute(action, new AsyncCallback<GetUserDetailResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(GetUserDetailResult result) {
				viewUserDialog.clear();
				viewUserDialog.preLoad(user, result.getUserDetail());
				addToPopupSlot(viewUserDialog);
				
			}
			
		});
		
	}

}
